package com.zhihao.takeaway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhihao.takeaway.common.utils.DateTool;
import com.zhihao.takeaway.common.utils.EasyUITable;
import com.zhihao.takeaway.common.utils.MD5Utils;
import com.zhihao.takeaway.entity.Dish;
import com.zhihao.takeaway.entity.Person;
import com.zhihao.takeaway.entity.User;
import com.zhihao.takeaway.mapper.UserMapper;
import com.zhihao.takeaway.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {
    @Override
    public User getUser(String userId) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getId,userId);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public Boolean isEnroll(Person person) {
        User  user = new User();
        user.setUsername(person.getUsername());
        user.setIdNumber(person.getIdNumber());
        user.setPhone(person.getPhone());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUsername,user.getUsername())
                .or().eq(User::getIdNumber,user.getIdNumber())
                .or().eq(User::getPhone,user.getPhone());
        List<User> list =  userMapper.selectList(queryWrapper);
        if(list.size() == 0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public String enroll(Person person) {
        User user = new User();
        user.setUsername(person.getUsername());
        user.setPhone(person.getPhone());
        user.setIdNumber(person.getIdNumber());
        user.setPassword(person.getPassword());
        user.setSex(person.getSex());
        user.setName(person.getName());
        user.setStatus(1);
//        进行md5加密
        String password = user.getPassword();
        user.setPassword(MD5Utils.inputPassToFormPass(password));
//        查询注册总数
        Integer num = userMapper.selectCount(null);
        String account =String.valueOf(num+10001);
        user.setAccount(account);
        userMapper.insert(user);
        return account;
    }

    @Autowired
    private UserMapper userMapper;


    @Override
    public User isUserExist(String account) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.lambda().eq(User::getAccount,account);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }
}
